package com.atlin.project.controller;

import com.atlin.project.annotation.AuthCheck;
import com.atlin.project.common.BaseResponse;
import com.atlin.project.common.DeleteRequest;
import com.atlin.project.common.ErrorCode;
import com.atlin.project.common.ResultUtils;
import com.atlin.project.contant.UserConstant;
import com.atlin.project.exception.BusinessException;
import com.atlin.project.exception.ThrowUtils;
import com.atlin.project.model.domain.SysUser;
import com.atlin.project.model.domain.BiProductsOrder;
import com.atlin.project.model.dto.biProduces.BiProductsOrderQueryRequest;
import com.atlin.project.service.SysUserService;
import com.atlin.project.service.BiProductsOrderService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * BI产品订单接口
 *
 * @author atlin
 */
@RestController
@RequestMapping("/biProductsOrder")
public class BiProductsOrderController {

    @Resource
    private BiProductsOrderService biProductsOrderService;

    @Resource
    private SysUserService sysUserService;

    private final static Gson GSON = new Gson();

    // region 增删改查
    

    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteBiProductsOrder(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        SysUser user = sysUserService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        BiProductsOrder oldbiProductsOrder = biProductsOrderService.getById(id);
        ThrowUtils.throwIf(oldbiProductsOrder == null, ErrorCode.NULL_ERROR);

        // 仅本人或管理员可删除
        if (!oldbiProductsOrder.getUserId().equals(user.getId()) && !sysUserService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean b = biProductsOrderService.removeById(id);
        return ResultUtils.success(b);
    }
    
    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<BiProductsOrder> getBiProductsOrderById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        BiProductsOrder biProductsOrder = biProductsOrderService.getById(id);
        if (biProductsOrder == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        SysUser loginUser = sysUserService.getLoginUser(request);
        // 不是本人或管理员，不能直接获取所有信息
        if (!biProductsOrder.getUserId().equals(loginUser.getId()) && !sysUserService.isAdmin(loginUser)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        return ResultUtils.success(biProductsOrder);
    }
    

    /**
     * 分页获取列表（仅管理员）
     *
     * @param biProductsOrderQueryRequest 查询参数
     * @param request              查询请求
     * @return 分页
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<BiProductsOrder>> listBiProductsOrderByPage(@RequestBody BiProductsOrderQueryRequest biProductsOrderQueryRequest,
                                                           HttpServletRequest request) {
        long current = biProductsOrderQueryRequest.getCurrent();
        long size = biProductsOrderQueryRequest.getPageSize();
        Page<BiProductsOrder> biProductsOrderPage = biProductsOrderService.page(new Page<>(current, size),
                biProductsOrderService.getQueryWrapper(biProductsOrderQueryRequest));
        return ResultUtils.success(biProductsOrderPage);
    }

    // endregion

}
